package com.tdam2012.grupo05;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class HistorialSMS extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_historial_sms);
        
        ListView lv = (ListView) findViewById(R.id.list);
        AccionSMSAdapter adapter = new AccionSMSAdapter(obtenerAcciones());     
        lv.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_historial_sms, menu);
        return true;
    }
    
    private ArrayList<AccionSMS> obtenerAcciones(){
        ArrayList<AccionSMS> acciones = new ArrayList<AccionSMS>();
        
        acciones.add(new AccionSMS("17/08/2012 11:40", "Mensaje 1"));
        acciones.add(new AccionSMS("19/08/2012 18:25", "Mensaje 2"));
        acciones.add(new AccionSMS("25/09/2012 15:16", "Mensaje 3"));
        
        return acciones;
    }
    
    class AccionSMS{
        String fechaHora;
        String resumen;
        
        public AccionSMS (String fechaHora, String resumen){
                this.fechaHora = fechaHora;
                this.resumen = resumen;
        }
        
        public String getFechaHora(){
                return this.fechaHora;
        }
        
        public String getResumen(){
                return this.resumen;
        }
    }
    
    class Holder{
        TextView txtFechaHora, txtResumen; 
    }
    
    class AccionSMSAdapter extends BaseAdapter{

        private ArrayList<AccionSMS> _acciones;
        private LayoutInflater _inflater;
        
        public AccionSMSAdapter(ArrayList<AccionSMS> acciones){
                _acciones = acciones;
                _inflater = LayoutInflater.from(HistorialSMS.this);
        }
        
                public int getCount() {
                        // TODO Auto-generated method stub
                        return _acciones.size();
                }

                public AccionSMS getItem(int position) {
                        // TODO Auto-generated method stub
                        return _acciones.get(position);
                }

                public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return position;
                }

                public View getView(int position, View convertView, ViewGroup parent) {
                        // TODO Auto-generated method stub
                        Holder holder = null;
                        if (convertView == null) {
                                convertView = _inflater.inflate(R.layout.item_lista, null);
                                holder = new Holder();
                                
                                holder.txtFechaHora = (TextView) convertView
                                                .findViewById(R.id.textView1);
                                
                                holder.txtResumen = (TextView) convertView
                                                .findViewById(R.id.textView2);
                                                                
                                convertView.setTag(holder);
                        } else {
                                holder = (Holder) convertView.getTag();
                        }

                        AccionSMS accion = _acciones.get(position);

                        holder.txtFechaHora.setText(accion.getFechaHora());
                        holder.txtResumen.setText(accion.getResumen());

                        return convertView;
                }
        
    }
}
